import Vue from "vue";
import VueRouter from "vue-router";
// 导入路由组件
import login from "@/views/login/index.vue";
import layout from "@/views/layout/index.vue";
import dashboard from "@/views/dashboard/index.vue";
import article from "@/views/article/index.vue";
// 导入vuex
import store from "@/store";
//导入饿了么ui 做轻提示
import { Message } from "element-ui";

Vue.use(VueRouter);

// 路由规则
const routes = [
  // 一级路由
  { path: "/login", component: login },
  {
    path: "/",
    redirect: "/layout",
    component: layout,
    children: [
      { path: "/", redirect: "/dashboard" },
      { path: "/article", component: article },
      { path: "/dashboard", component: dashboard },
    ],
  },
];

const router = new VueRouter({
  routes,
});

// 路由守卫
router.beforeEach((to, form, next) => {
  if (to.path === "/login" || store.state.user.token) {
    next();
  } else {
    Message.error("未登录");
    next("/login");
  }
});
export default router;
